package com.ruoyi.quartz.task;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Data;
import com.ruoyi.system.domain.TStation;
import com.ruoyi.system.mapper.DataMapper;
import com.ruoyi.system.mapper.TStationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.List;
import java.util.Random;

@Component("myTask")
public class MyTask
{
    @Autowired
    TStationMapper stationMapper;

    @Autowired
    DataMapper dataMapper;
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }
    // 随机数生成器
    Random random = new Random();

    /**
     * 生成模拟数据
     */
    public void generateData()
    {

        List<TStation> stations = stationMapper.getAll();
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        // 将分钟和秒设置为 0
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        for(int i =0;i<stations.size();i++){
            TStation station = stations.get(i);
            Data data = new Data();
            data.setStationId(station.getStationId());
            data.setTime(calendar.getTime());

            // 模拟温度（考虑当前日期和经纬度）
            data.setTemperature(generateTemperature(calendar, station.getLat(), station.getLon()));

            // 模拟降雨量（考虑当前日期和经纬度）
            data.setRain(generateRain(calendar, station.getLat(), station.getLon()));

            // 模拟气压（考虑当前日期和经纬度）
            data.setPressure(generatePressure(calendar, station.getLat(), station.getLon()));

            // 模拟湿度（考虑当前日期和经纬度）
            data.setHumidity(generateHumidity(calendar, station.getLat(), station.getLon()));

            data.setWater(generateWater(calendar, station.getLat(), station.getLon(), data.getRain()));

            System.out.println("站点 "+station.getStationName() + " 生成 " + data.getTime() + " 模拟数据完成");
//            dataMapper.insertData(data);
        }
    }

    private Double generateTemperature(Calendar calendar, double lat, double lon) {
        // 基础温度公式：考虑季节和纬度
        int month = calendar.get(Calendar.MONTH) + 1;
        Double baseTemperature = Double.valueOf(15 + (month / 12f) * 15); // 基础温度随月份变化
        baseTemperature += (Math.sin(Math.toRadians(lat)) * 5); // 纬度影响

        // 随机波动
        Double randomTemperature = (baseTemperature + (random.nextFloat() * 5 - 2.5));
        return randomTemperature;
    }

    private Double generateRain(Calendar calendar, double lat, double lon) {
        // 基础降雨量公式：考虑季节和经度
        int month = calendar.get(Calendar.MONTH) + 1;
        Double baseRain = Double.valueOf((month / 12f) * 10); // 基础降雨量随月份变化
        baseRain += (Math.cos(Math.toRadians(lon)) * 5); // 经度影响

        // 随机波动
        Double randomRain = Double.valueOf(baseRain + (random.nextFloat() * 10 - 5));
        return randomRain>=0?randomRain:0;
    }

    private Double generatePressure(Calendar calendar, double lat, double lon) {
        // 基础气压公式：考虑季节和纬度
        int month = calendar.get(Calendar.MONTH) + 1;
        Double basePressure = Double.valueOf(1000 + (month / 12f) * 20 - 10); // 基础气压随月份变化
        basePressure +=  (Math.sin(Math.toRadians(lat)) * 10); // 纬度影响

        // 随机波动
        Double randomPressure = basePressure + (random.nextFloat() * 40 - 20);
        return randomPressure;
    }

    private Double generateHumidity(Calendar calendar, double lat, double lon) {
        // 基础湿度公式：考虑季节和经度
        int month = calendar.get(Calendar.MONTH) + 1;
        Double baseHumidity = Double.valueOf(30 + (month / 12f) * 50 - 25); // 基础湿度随月份变化
        baseHumidity += (Math.cos(Math.toRadians(lon)) * 10); // 经度影响

        // 随机波动
        Double randomHumidity = baseHumidity + (random.nextFloat() * 50 - 25);
        return randomHumidity>=0?randomHumidity:0;
    }
    private Double generateWater(Calendar calendar, double lat, double lon, Double rain) {
        int month = calendar.get(Calendar.MONTH) + 1;
        Double baseWater = Double.valueOf(100 + (month / 12f) * 50); // 基础水位随月份变化
        baseWater += (Math.sin(Math.toRadians(lat)) * 20); // 纬度影响
        baseWater += (Math.cos(Math.toRadians(lon)) * 10); // 经度影响
        baseWater += (rain * 0.1); // 降雨量对水位的影响

        Double randomWater = baseWater + (random.nextFloat() * 10 - 5);
        return randomWater >= 0 ? randomWater : 0;
    }
}